﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataCom;
using System.Data;
using System.Xml;
using XmlDataCommunication.Properties;

namespace XmlDataCommunication
{
    public class XmlProductLogDataAccess : IProductLogAccess
    {
        public DataRow GetProductLogByDateLineAndShift(string date, string lineCode, string shift)
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNode node = doc.SelectSingleNode("//R4_ProductLog/Record[Line_Code='" + lineCode + "' and LogDate='" + date + "' and Shift='" + shift + "']");
            if (node != null) return GeneralFunctions.GetDataRowByXmlNode(node);
            else return null;
        }

        #region IProductLogAccess Members


        public DataTable GetProductLog(string from, string to, string line, string shift)
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlDocument docResult = new XmlDocument();
            docResult.LoadXml("<Records></Records>");
            string condition = string.Empty;
            if (line != null) condition += "Line_Code='" + line + "'";
            if (shift != null)
            {
                if (!string.IsNullOrWhiteSpace(condition)) condition += " and ";
                condition += "Shift='" + shift + "'";
            }
            if (condition.Length > 0) condition = "[" + condition + "]";
            XmlNodeList nodes = doc.SelectNodes(string.Format("//R4_ProductLog/Record{0}", condition));
            foreach (XmlNode node in nodes)
            {
                string logDate = node.SelectSingleNode("./LogDate").InnerText;
                if (DateTime.Parse(logDate) >= DateTime.Parse(from) && DateTime.Parse(logDate) <= DateTime.Parse(to))
                    docResult.FirstChild.AppendChild(docResult.ImportNode(node, true));
            }
            return GeneralFunctions.GetDataTableByXmlNodes(docResult.FirstChild.ChildNodes);
        }

        #endregion

        /// <summary>
        /// Update Product Log By Key
        /// </summary>
        /// <param name="lineCode"></param>
        /// <param name="logDate"></param>
        /// <param name="shift"></param>
        /// <param name="planNum"></param>
        /// <param name="nowPlan"></param>
        /// <param name="productNum"></param>
        /// <param name="stopTime"></param>
        public void UpdateProductLogByKey(string lineCode, 
                                          string logDate, 
                                          string shift,
                                          string planNum,
                                          string nowPlan,
                                          string productNum,
                                          string stopTime)
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNode node = doc.SelectSingleNode("//R4_ProductLog/Record[Line_Code='" + lineCode + "' and LogDate='" + logDate + "' and Shift='" + shift + "']");
            
            if (node == null)
            {
                List<string> columnNames = new List<string>()
                {
                    "Line_Code","LogDate","Shift","PlanNum","NowPlan","ProductNum","StopTime"
                };
                node = GeneralFunctions.CreateRecordInTable(doc, "R4_ProductLog", columnNames);
                node.SelectSingleNode("./Line_Code").InnerText = lineCode;
                node.SelectSingleNode("./LogDate").InnerText = logDate;
                node.SelectSingleNode("./Shift").InnerText = shift;
            }

            node.SelectSingleNode("./PlanNum").InnerText = planNum;
            node.SelectSingleNode("./NowPlan").InnerText = nowPlan;
            node.SelectSingleNode("./ProductNum").InnerText = productNum;
            node.SelectSingleNode("./StopTime").InnerText = stopTime;

            GeneralFunctions.SaveXmlDocument(doc);
        }
    }
}
